package com.itheima.health.service.impl;

import com.itheima.health.dao.OrderSettingDao;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author spt
 * @description
 * @date 2019/9/29
 **/
@Service
@Slf4j
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;


    @Override
    //查看预约人数
    public List<Map> getOrderSettingByMonth(String year, String month) {
        //开始时间
        String dateStart = year + "-" + month + "-1";
        log.info("dateStart" + dateStart);
        //结束时间
        String dateLen = year + "-" + month + "-31";
        log.info("dateLen" + dateLen);
        //  调用查询方法
        List<OrderSetting> orderSettingByMonth = orderSettingDao.getOrderSettingByMonth(dateStart, dateLen);


        List<Map> data = new ArrayList<>();

        for (OrderSetting setting : orderSettingByMonth) {
            Map<String, Object> map = new HashMap<>();
            map.put("date", setting.getOrderDate().getDate());
            map.put("number", setting.getNumber());
            map.put("reservations", setting.getReservations());

            data.add(map);
            log.info("map" + map);
        }

        return data;
    }

    //单独设置预约人数
    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        log.info("orderSetting" + orderSetting.getOrderDate());
        int number = orderSetting.getNumber();
        Date orderDate = orderSetting.getOrderDate();
        //判断是否有创建过
        int date = orderSettingDao.findNumberAndDate(orderDate);
        log.info("flag" + date);
        if (date >0) {
            orderSettingDao.editNumberByDate(number, orderDate);
        }else {
            orderSettingDao.addNumberAndDate(number,orderDate);
        }
    }

    //设置批量预约人数
    @Override
    public void upload(List<OrderSetting> orderSettingList) {
        log.info("OrderSettingServiceImpl设置批量预约人数");
       //单独设置预约人数 一模一样
        for (OrderSetting orderSetting : orderSettingList) {
            int number = orderSetting.getNumber();
            Date orderDate = orderSetting.getOrderDate();
            //判断是否有创建过
            int date = orderSettingDao.findNumberAndDate(orderDate);
            log.info("flag" + date);
            if (date >0) {
                orderSettingDao.editNumberByDate(number, orderDate);
            }else {
                orderSettingDao.addNumberAndDate(number,orderDate);
            }

        }
    }
    /**
     * 剩余可预约人数
     * @return 返回查询到的剩余可预约人数
     */
    @Override
    public int getNumber(Date orderDate) {

        return orderSettingDao.getNumber(orderDate);
    }

    /**
     * 更新可预约人数
     */
    @Override
    public void updaraNumber(Date orderDate) {

        orderSettingDao.updataNumber(orderDate);
    }

}
